<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/5/16 13:37
 * Note: 分类管理-栏目
 */
namespace Admin\Controller;
use Think\Controller;
use Think\Model;

class CateController extends CommonController {
    //分类列表
    public function index(){
        $Cate = M('Cate'); // 实例化User对象
        $count = $Cate->where('status=1')->count();// 查询满足要求的总记录数
        $Page  = new \Think\Page($count,10);// 实例化分页类 传入总记录数和每页显示的记录数(10)
        $show  = $Page->show();// 分页显示输出
        // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
        $list  = $Cate->where('status=1')->order('sort asc,id desc')->limit($Page->firstRow.','.$Page->listRows)->select();

        foreach($list as &$v){
            $v['time'] = date("Y-m-d H:i:s",$v['time']);
            $v['type'] = $v['type']=='0' ? '内容' : '域名' ;
        }
        $this->assign('list',$list);// 赋值数据集
        $this->assign('page',$show);// 赋值分页输出
        $this->assign('title', '分类列表');
        $this->display();
    }
    //分类修改
    public function edit(){
        $id = I('get.id','','intval');
        $info = M('Cate')->where(array('id'=>$id))->find();
        if(IS_POST){
            $id = I('post.id','','intval');
            $title = I('post.title','','trim');
            $author = I('post.author','','trim');
            $type = I('post.type','','intval');
            if($type !== 0){
                $url = I('post.url','','trim');
            }else{
                $url = '';
            }
            $data = array(
                'title'  => $title ,
                'author' => $author ,
                'time'   => time() ,
                'id'     => $id ,
                'type'   => $type ,
                'url'    => $url ,
            );
            $result = M('Cate')->where(array('id'=>$id))->save($data);
            if(!$result) {
                $this->error('更新失败！', U('Cate/edit'));
            }else{
                $this->success('更新成功', U('Cate/index'));
            }
            return;
        }
        $this->assign('info', $info);
        $this->assign('title', '编辑分类');
        $this->display();
    }
    //分类删除
    public function del(){
        $id = I('get.id','','intval');
        $info = M('Article')->where(array('cateid'=>$id))->select();
        if(!info){
            $this->error('该栏目下还有文章内容，不能删除该栏目！');
        }else{
            $result = M('Cate')->where(array('id'=>$id))->save(array('status'=>'0'));
            if(!$result){
                $this->error('删除失败', U('Cate/index'));
            }else{
                $this->success('删除成功', U('Cate/index'));
            }
            return;
        }
    }
    //新增分类
    public function plus(){
        if(IS_POST){
            $title = I('post.title','','trim');
            if(M('Cate')->where(array('title'=>$title))->find()){
                $this->error('分类名称已存在！', U('Cate/plus'));
            }
            $author = I('post.author','','trim');
            $author = !empty($author) ? $author : 'admin';
            $type = I('post.type','','intval');
            if($type !== 0){
                $url = I('post.url','','trim');
            }else{
                $url = '';
            }
            $data = array(
                'title'  => $title ,
                'author' => $author ,
                'time'   => time() ,
                'type'   => $type ,
                'url'    => $url ,
            );
            $result = M('Cate')->add($data);
            if(!$result){
                $this->error('新增失败', U('Cate/index'));
            }else{
                $this->success('新增成功', U('Cate/index'));
            }
            return;
        }
        $this->assign('title', '新增分类');
        $this->display();
    }
    //分类排序
    public function sort(){
        foreach($_POST as $id=>$sort){
            M('Cate')->where(array('id'=>$id))->setField('sort',$sort);
        }
        $this->success('新增排序成功', U('Cate/index'));
    }
}